//MINI UI解析
mini.parse();

//请求配置处理器类
function ReqCfgHandler() {
	this.srcName = mini.get("srcName");
	this.srcState = mini.get("srcState");
	this.reqCfgGrid = mini.get("reqCfgGrid");
	
	this.isExpanded = false;
	this.rcCtl = mini.get("rcCtl");
	this.tbForm = $("#toolbarForm");
	this.rcPanel = mini.get("reqConfigPanel");
	
	this.wmCfg = new WebMgrConfig();
	this.queryName = "";
	this.queryState = this.wmCfg.normalState[0].value;
	
	this.reqCfgGrid.url = __root + "/getRequestConfigs.do";
	
	this.loadSrcState = function() {
		this.srcState.setData(this.wmCfg.normalState);
		this.srcState.setValue(this.wmCfg.normalState[0].value);
	};
	
	this.loadReqCfgGrid = function() {
		this.reqCfgGrid.load({ srcName : this.queryName, srcState : this.queryState });
	};
	
	this.findState = function(value) {
		var state = null;
		
		for (var index = 0; index < this.wmCfg.normalState.length; index += 1) {
			var temp = this.wmCfg.normalState[index];
			if (temp.value == value) {
				state = temp;
				break;
			}
		}
		
		return state;
	};
	
	this.changeToYes = function(id) {
		this.switchState(id, true);
	};
	
	this.changeToNo = function(id) {
		this.switchState(id, false);
	};
	
	this.switchState = function(id, to) {
		var param = { srcId : id, srcTo : to};
		var json = mini.encode(param);
		$.ajax({
			url : __root + "/switchRcState.do", 
			type : "POST", 
			data: {
	        	data: json,
	        	lslUser: gbOperateUser,
	    		lslAddress: gbOperateIp
	        },
			dataType : "json", 
			success : function(result) {
				instance.loadReqCfgGrid();
			}, 
			error : function (request) {
				mini.alert(request.responseText, "系统提示");
			}
		});
	};
	
	this.edit = function(id, name) {
		miniWin.afreshNewWin();
		miniWin.setParams(342, 232, "编辑配置[<font color='blue'>" + name + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		
		miniWin.load("editReqCfg.jsp", function() {
			var iframe = miniWin.getIFrameEl();
	        var data = { srcId : id };
	        iframe.contentWindow.instance.load(data);
		}, function(action) {
			if (action == "save") {
				instance.loadReqCfgGrid();
			}
		});
		
		miniWin.show();
	};
	
	this.del = function(id) {
		mini.confirm("确定要删除选定的配置？", "系统提示", function(action) {
			if (action == "ok") {
				var ids = [{ srcId : id }];
				instance.removes(ids);
			}
		});
	};
	
	this.removes = function(ids) {
		var json = mini.encode(ids);
		$.ajax({
			url : __root + "/delRequestConfigs.do", 
			type : "POST", 
			data: {
	        	data: json,
	        	lslUser: gbOperateUser,
	    		lslAddress: gbOperateIp
	        },
			dataType : "json", 
			success : function(result) {
				instance.loadReqCfgGrid();
			}, 
			error : function (request) {
				mini.alert(request.responseText, "系统提示");
			}
		});
	};
	
	this.doLayout = function() {
		instance.rcPanel.doLayout();
	};
};

var instance = new ReqCfgHandler();		//创建请求配置处理器全局实例
instance.loadSrcState();				//加载配置状态
instance.loadReqCfgGrid();				//加载配置表格

//定义新建配置点击事件处理程序
function onRcNewClick(e) {
	miniWin.afreshNewWin();
	miniWin.setParams(342, 232, "新建配置");
	miniWin.setOption("bodyStyle","padding:0px;");
	
	miniWin.load("editReqCfg.jsp", function() {
		var iframe = miniWin.getIFrameEl();
        var data = { srcId : null };
        iframe.contentWindow.instance.load(data);
	}, function(action) {
		if (action == "save") {
			instance.loadReqCfgGrid();
		}
	});
	
	miniWin.show();
}

//定义查询控制点击事件处理程序
function onRcCtlClick(e) {
	if (instance.isExpanded) {
		instance.isExpanded = false;
		instance.rcCtl.setText("展开查询");
		instance.rcCtl.setIconCls("icon-btn-expand");
	} else {
		instance.isExpanded = true;
		instance.rcCtl.setText("折叠查询");
		instance.rcCtl.setIconCls("icon-btn-collapse");
	}
	
	instance.tbForm.toggle(500);
	setTimeout(instance.doLayout, 500);
}

//定义查询点击事件处理程序
function onRcQueryClick(e) {
	instance.queryName = instance.srcName.getValue();
	instance.queryState = instance.srcState.getValue();
	
	instance.loadReqCfgGrid();
}

//定义刷新点击事件处理程序
function afresh(e) {
	//执行查询
	onRcQueryClick();
	//重置布局
	instance.doLayout();
}

//定义同步设置点击事件处理程序
function onRcSynClick(e) {
	miniWin.afreshNewWin();
	miniWin.setParams(342, 160, "同步设置");
	miniWin.setOption("bodyStyle","padding:0px;");
	
	miniWin.load("syncSetting.jsp", function() {
		var iframe = miniWin.getIFrameEl();
		var data = { syncType : instance.wmCfg.syncType.reqcfg };
        iframe.contentWindow.instance.load(miniWin, data);
	});
	
	miniWin.show();
}

//定义请求配置表格单元格绘制事件处理程序
function onReqCfgGridDrawCell(e) {
	var html = "";
	
	switch (e.column.field) {
		case "srcState":
		{
			var state = instance.findState(e.record.srcState);
			if (state) {
				html += "<span><div class='" + state.cls + "' title='" + state.text + 
					"' onclick='instance." + state.method + "(\"" + e.record.srcId + "\");'></div></span>";
			}
			break;
		}
		case "srcOperator":
		{
			var state = instance.findState(e.record.srcState);
			if (state) {
				var id = e.record.srcId;
				
				html += "<span><a style='cursor: pointer;' onclick='instance.edit(\"" + id + 
					"\", \"" + e.record.srcName + "\");'>编辑</a></span>";
				html += "<span>&nbsp;|&nbsp;</span>";
				if (state.deleted) {
					html += "<span><a style='cursor: pointer;' onclick='instance.del(\"" + id + "\");'>删除</a></span>";
				} else {
					html += "<span><a style='cursor: default; color: gray;'>删除</a></span>";
				}
			}
			break;
		}
	}
	
	if (html) e.cellHtml = html;
}
